/*
 * JTurboFileGUI.java
 *
 * Created on 27. august 2008, 16:42
 */

package jturbofile.gui;

import java.util.List;
import java.util.Vector;
import jturbofile.Config;
import jturbofile.net.NetCore;
import jturbofile.net.PeerThread;

/**
 *
 * @author  Rolf
 */
public class JTurboFileGUI extends javax.swing.JFrame {

    private static JTurboFileGUI singleton;
    
    private UserListUI userListUI;
    
    public static JTurboFileGUI singleton() {
        if( singleton == null) {
            singleton = new JTurboFileGUI();
        }
        
        return singleton;
    }
    
    /** Creates new form JTurboFileGUI */
    private JTurboFileGUI() {
        initComponents();
        
        Config conf = Config.singleton();
        
        setBounds(conf.getXPos(), conf.getYPos(), this.getWidth(), this.getHeight());
        
        userListUI = new UserListUI();
        
        jTabPane.addTab("ConnectedUsers", userListUI);
        jTabPane.addTab("Log", LogPanel.singleton());
        jTabPane.addTab("Config", new ConfigPanel());
        
        reMakeHostsList();
        
        updateUsers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabPane = new javax.swing.JTabbedPane();
        jLabel1 = new javax.swing.JLabel();
        jIp = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jOldHosts = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setTitle("JTurboFile");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
        });

        jLabel1.setText("Host:");

        jButton1.setText("Connect");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jOldHosts.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Saved Hosts:");

        jButton2.setText("Connect");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jIp, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(76, 76, 76)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jOldHosts, 0, 270, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel2)
                    .addComponent(jButton2)
                    .addComponent(jOldHosts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
    Config conf = Config.singleton();
    conf.setXPos(this.getX());
    conf.setYPos(this.getY());
}//GEN-LAST:event_formComponentMoved

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String ip = jIp.getText();
    NetCore.singleton().connectToPeer(ip);

    List<String> hosts = Config.singleton().getHosts();
    
    if( !hosts.contains(ip) ) {
        hosts.add(ip);
        Config.singleton().setHosts(hosts);
        
        reMakeHostsList();
    }
    
    jIp.setText("");
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String ip = (String) jOldHosts.getSelectedItem();
    NetCore.singleton().connectToPeer(ip);
}//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jIp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox jOldHosts;
    private javax.swing.JTabbedPane jTabPane;
    // End of variables declaration//GEN-END:variables

    public synchronized void updateUsers() {
        
        userListUI.reset();
        
        for(PeerThread pt : NetCore.singleton().getPeers().values()) {
            userListUI.addUser(pt);
        }
        
        userListUI.updateUsers();
        
        this.getContentPane().validate();
    }

    private void reMakeHostsList() {
        jOldHosts.removeAllItems();
        
        for(String s : Config.singleton().getHosts()) {
            jOldHosts.addItem(s);
        }
    }

}
